package 华为OD机试B卷复用题.进阶题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HJ9_最长广播响应 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试B卷复用题/进阶题/input/9.txt"));
        MyUtil.START();
        Solution(in);
        MyUtil.END();
    }

    static boolean[][] edges;
    static boolean[] isVisited;
    static int maxDepth = -1;

    static void Solution(Scanner in) {
        int N = in.nextInt();
        int edge_num = in.nextInt();
        edges = new boolean[N + 1][N + 1];
        isVisited = new boolean[N + 1];
        for (int i = 0; i < edge_num; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            edges[a][b] = true;
            edges[b][a] = true;
        }
        int start = in.nextInt();
        Queue<Integer> queue = new ArrayDeque<>();
        queue.add(start);
        while (!queue.isEmpty()) {
            maxDepth++;
            List<Integer> nums = new ArrayList<>();
            while (!queue.isEmpty()) {
                int tmp = queue.poll();
                isVisited[tmp] = true;
                nums.add(tmp);
            }
            for (int i = 0; i < nums.size(); i++) {
                int now = nums.get(i);
                for (int j = 1; j <= N; j++) {
                    if (!isVisited[j] && edges[now][j]) {
                        queue.add(j);
                    }
                }
            }
        }
        System.out.println(maxDepth * 2);
    }
}
